<template>
  <div>
    <el-form :inline="true" :model="dataForm">
      <el-form-item>
        <el-input v-model="dataForm.attrGroupName" placeholder="组名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getAttrGroup()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="dialogFormVisible = true">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="attrGroupList"
      stripe
      border
      style="width: 100%"
      highlight-current-row>

      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="attrGroupId" header-align="center" align="center" label="分组id"></el-table-column>
      <el-table-column prop="attrGroupName" header-align="center" align="center" label="组名"></el-table-column>
      <el-table-column prop="sort" header-align="center" align="center" label="排序"></el-table-column>
      <el-table-column prop="descript" header-align="center" align="center" label="描述"></el-table-column>
      <el-table-column prop="icon" header-align="center" align="center" label="组图标"></el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="relationHandle(scope.row.attrGroupId)">关联</el-button>
          <el-button type="text" size="small" @click="updateHandle(scope.row.attrGroupId)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.attrGroupId)">删除</el-button>
        </template>
      </el-table-column>

    </el-table>

    <!-- 分页 -->
    <el-pagination
      background
      :current-page="page"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change=""
    />

    <!-- 新增 -->
    <el-dialog title="属性分组" :visible.sync="dialogFormVisible">
      <el-form :model="attrGroup">
        <el-form-item label="组名" :label-width="formLabelWidth">
          <el-input v-model="attrGroup.attrGroupName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序" :label-width="formLabelWidth">
          <el-input v-model="attrGroup.sort" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="描述" :label-width="formLabelWidth">
          <el-input v-model="attrGroup.descript" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="组图标" :label-width="formLabelWidth">
          <el-input v-model="attrGroup.icon" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false, addAttrGroup()">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 关联信息 -->
    <el-dialog :close-on-click-modal="false" :visible.sync="visible">
      <div>
        <el-form :inline="true" :model="attrForm" @keyup.enter.native="">
          <el-form-item>
            <el-input v-model="attrForm.key" placeholder="参数名" clearable></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="">查询</el-button>
            <el-button type="primary" @click="addRelation">新增关联</el-button>
          </el-form-item>
        </el-form>
        <el-table :data="attrAttrGroupList" border v-loading="dataListLoading" style="width: 100%;">
          <el-table-column prop="attrId" header-align="center" align="center" label="属性id"></el-table-column>
          <el-table-column prop="attrName" header-align="center" align="center" label="属性名"></el-table-column>
          <el-table-column prop="icon" header-align="center" align="center" label="属性图标"></el-table-column>
        </el-table>

        <!-- 新增关联 -->
        <el-dialog :visible.sync="innerVisible" append-to-body>
          <div>
            <el-table :data="attrList" border v-loading="dataListLoading" style="width: 100%;"@selection-change="handlerAttrSelect">
              <el-table-column type="selection" header-align="center" align="center"></el-table-column>
              <el-table-column prop="attrId" header-align="center" align="center" label="属性id"></el-table-column>
              <el-table-column prop="attrName" header-align="center" align="center" label="属性名"></el-table-column>
              <el-table-column prop="icon" header-align="center" align="center" label="属性图标"></el-table-column>
            </el-table>
            <el-pagination
              :current-page="pageIndex"
              :page-size="pageSize"
              :total="totalPage"
              layout="total, sizes, prev, pager, next, jumper"
              @current-change="getAttrInfo"
            ></el-pagination>
            <div slot="footer" class="dialog-footer">
              <el-button @click="innerVisible = false">取 消</el-button>
              <el-button type="primary" @click="addRelationHandler">确认新增</el-button>
            </div>
          </div>
        </el-dialog>
      </div>
    </el-dialog>

    <!-- 修改属性分组信息 -->
    <el-dialog :close-on-click-modal="false" :visible.sync="visibleUpdate">
      <div>
        <el-form :model="attrGroupUpdate">
          <el-form-item label="组名" :label-width="formLabelWidth">
            <el-input v-model="attrGroupUpdate.attrGroupName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="排序" :label-width="formLabelWidth">
            <el-input v-model="attrGroupUpdate.sort" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="描述" :label-width="formLabelWidth">
            <el-input v-model="attrGroupUpdate.descript" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="组图标" :label-width="formLabelWidth">
            <el-input v-model="attrGroupUpdate.icon" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visibleUpdate = false">取 消</el-button>
        <el-button type="primary" @click="updateAttrGroup">确认</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import attrGroup from "@/api/game/attrGroup";
import attrAttrGroupRela from "@/api/game/attrAttrGroupRela";
import attr from "@/api/game/attr";
import qs from 'qs'

export default {
  //import引入的组件需要注入到对象中才能使用
  props: {},
  data() {
    return {
      dataForm: {},
      attrForm: {},
      attrGroupList: [], //游戏列表
      dataListLoading: false,
      innerVisible: true,
      attrGroup: {},
      attrAttrGroupList: {},
      formLabelWidth: '150',
      visible: false,
      dialogFormVisible: false,
      visibleUpdate: false,
      attrGroupUpdate: {},
      attrList: [],  //游戏配置参数细腻
      attrIdList: [],
      attrGroupId: '',
      page: 1,  //当前页码
      limit: 20,  // 每页显示的商品数量
      total: 0,  // 商品总量
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
    };
  },
  activated() {
    this.getAttrGroup()
  },
  methods: {
    getAttrGroup(page = 1) {
      this.page = page
      const params = {
        page: this.page,
        key: this.dataForm.attrGroupName
      }
      attrGroup.getAttrGroupInfo(params)
        .then(result => {
          this.total = result.data.totalCount
          this.attrGroupList = result.data.list
        })
    },

    //添加属性分组
    addAttrGroup() {
      attrGroup.addAttrGroupInfo(this.attrGroup)
        .then(result => {
          this.getAttrGroup()
        })
    },

    //获取关联信息
    relationHandle(val) {
      attrAttrGroupRela.getAttrInfoByAttrGroupId(val)
        .then(result => {
          this.attrAttrGroupList = result.data
          this.attrGroupId = val
          this.visible = true
        })
    },

    //修改属性分组信息
    updateHandle(val) {
      attrGroup.getAttrGroupInfoByAttrGroupId(val)
        .then(result => {
          this.attrGroupUpdate = result.data
          this.visibleUpdate = true
        })
    },

    //修改
    updateAttrGroup() {
      attrGroup.updateAttrGroupInfo(this.attrGroupUpdate)
        .then(result => {
          this.visibleUpdate = false
          this.attrGroupUpdate = {}
          this.getAttrGroup()
        })
    },

    //删除
    deleteHandle(val) {
      this.$confirm(
        `确定对[id = ${val}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(result => {
        attrGroup.deleteAttrGroupInfo(val)
          .then(result => {
            this.getAttrGroup()
          })
      })
    },

    getAttrInfo(pageIndex = 1) {
      this.pageIndex = pageIndex
      const params = {
        page: this.pageIndex
      }
      attr.getAttrInfo(params)
        .then(result => {
          this.total = result.data.totalCount
          this.attrList = result.data.list
          console.log(this.attrList)
        })
    },

    addRelation() {
     this.getAttrInfo()
      this.innerVisible = true
    },

    handlerAttrSelect(val) {
      this.attrIdList = []
      for (let selectItem of val) {
        this.attrIdList.push(selectItem.attrId)
      }
    },

    //新增关联关系
    addRelationHandler() {
      const params = {
        ids: this.attrIdList + '',
        groupId: this.attrGroupId
      }
      attrAttrGroupRela.addAttrAttrGroupRela(params)
      .then(result => {
        console.log(result)
      })
    }
  }
};
</script>
